<?php

class Promo_Dao{

	function __construct(){	}
	
	function get_all(){
		$sql="
		select *
		from
		promo
		";
		
		$list_promo = array();
		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$promo = new Promo();
				
				$promo->id_promo = $row['ID_PROMO'];
				$promo->id_produk = $row['ID_PRODUK'];
				$promo->tgl_mulai = $row['TGL_MULAI'];
				$promo->tgl_selesai = $row['TGL_SELESAI'];
				$promo->harga_promo = $row['HARGA_PROMO'];
				$promo->keterangan_promo = $row['KETERANGAN'];
				$promo->id = $row['ID'];
				$list_promo[] = $promo;
			}
		}	
		return $list_promo;
	}
	
	function get_id($id){
		$sql="
		select *
		from
		promo
		WHERE
		ID_PROMO = '".$id."'
		";
		
		$promo=false;		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$promo = new Promo();
				$promo->id_promo = $row['ID_PROMO'];
				$promo->id_produk = $row['ID_PRODUK'];
				$promo->tgl_mulai = $row['TGL_MULAI'];
				$promo->tgl_selesai = $row['TGL_SELESAI'];
				$promo->harga_promo = $row['HARGA_PROMO'];
				$promo->keterangan_promo = $row['KETERANGAN'];
				$promo->id = $row['ID'];
			}
		}	
		return $promo;
	}
	
	function get_idmember($id){
		$sql="
		select *
		from
		promo
		WHERE
		ID = '".$id."'
		";
		
		$list_promo = array();
		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$promo = new Promo();
				
				$promo->id_promo = $row['ID_PROMO'];
				$promo->id_produk = $row['ID_PRODUK'];
				$promo->tgl_mulai = $row['TGL_MULAI'];
				$promo->tgl_selesai = $row['TGL_SELESAI'];
				$promo->harga_promo = $row['HARGA_PROMO'];
				$promo->keterangan_promo = $row['KETERANGAN'];
				$promo->id = $row['ID'];
				$list_promo[] = $promo;
			}
		}	
		return $list_promo;
	}
	
	function ubah_promo(Promo $promo){
		//echo $promo->id_produk;
		//echo $promo->tgl_mulai;
		$sql="update promo set 
		ID_PRODUK='$promo->id_produk', 
		TGL_MULAI='$promo->tgl_mulai',
		TGL_SELESAI='$promo->tgl_selesai',
		HARGA_PROMO='$promo->harga_promo',
		KETERANGAN='$promo->keterangan_promo'
		where ID_PROMO='$promo->id_promo'";
		$query=mysql_query($sql);
	}
	
	function tambah_promo(Promo $promo){
		//echo $promo->id_produk;
		//echo $promo->tgl_mulai;
		$sql="insert into promo
		values('',
		'$promo->id_produk', 
		'$promo->tgl_mulai',
		'$promo->tgl_selesai',
		'$promo->harga_promo',
		'$promo->keterangan_promo',
		'$promo->id')
		";
		$query=mysql_query($sql);
	}
	
	function hapus_promo(Promo $promo){

		$sql="delete from promo
		WHERE
		ID_PROMO = '".$promo->id_promo."'
		";
		$query=mysql_query($sql);
	}
	
	
	function get_aktif(){
		$sql="
		select *
		from
		promo
		WHERE
		TGL_SELESAI >= CURDATE()
		ORDER BY TGL_SELESAI
		";
		
		$list_promo = array();
		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$promo = new Promo();
				
				$promo->id_promo = $row['ID_PROMO'];
				$promo->id_produk = $row['ID_PRODUK'];
				$promo->tgl_mulai = $row['TGL_MULAI'];
				$promo->tgl_selesai = $row['TGL_SELESAI'];
				$promo->harga_promo = $row['HARGA_PROMO'];
				$promo->keterangan_promo = $row['KETERANGAN'];
				$promo->id = $row['ID'];
				$list_promo[] = $promo;
			}
		}	
		return $list_promo;
	}
	
	function get_aktif_top12(){
		$sql="
		select *
		from
		promo
		WHERE
		TGL_SELESAI >= CURDATE()
		LIMIT 12
		";
		
		$list_promo = array();
		
		$data = mysql_query($sql);
		if($data){
			while($row = mysql_fetch_assoc($data)){
			
				$promo = new Promo();
				
				$promo->id_promo = $row['ID_PROMO'];
				$promo->id_produk = $row['ID_PRODUK'];
				$promo->tgl_mulai = $row['TGL_MULAI'];
				$promo->tgl_selesai = $row['TGL_SELESAI'];
				$promo->harga_promo = $row['HARGA_PROMO'];
				$promo->keterangan_promo = $row['KETERANGAN'];
				$promo->id = $row['ID'];
				$list_promo[] = $promo;
			}
		}	
		return $list_promo;
	}

}


class Promo{
	var $id_promo;
	var $id_produk;
	var $tgl_mulai;
	var $tgl_selesai;
	var $harga_promo;
	var $keterangan_promo;
	var $id;
}